package com.ldj.common;
import java.sql.*;

public class DatabaseBean {
 private static String driverName = "com.microsoft.jdbc.sqlserver.SQLServerDriver";

 private static String username = "sa";

 private static String password = "123456";

 private static String databaseURL = "jdbc:microsoft:sqlserver://localhost:1433;DatabaseName=sonyinner;characterEncoding=UTF-8";

 /**
  * 获取数据库连接Connection对象
  * 无法获取则返回null
  * @return
  */
 public static Connection getConnection() {
  Connection conn = null;
  try {
  // 加载驱动程序
  Class.forName(driverName);
  // 获取数据库连接
  conn = DriverManager.getConnection(databaseURL, username, password);
  } catch (ClassNotFoundException cnfe) {
  // 无法找到驱动程序
  cnfe.printStackTrace();
  conn = null;
  } catch (SQLException sqle) {
  // 数据库发生异常
  sqle.printStackTrace();
  conn = null;
  }
  return conn;
 } 

 /**
  * 关闭连接
  * 按次序释放资源
  * @param conn
  * @param stmt
  * @param rs
  */
 public static void close(Connection conn, PreparedStatement pstmt, ResultSet rs) {
  try {
  if (rs != null)
  rs.close();
  } catch (Exception rse) {
  rse.printStackTrace();
  }

  try {
  if (pstmt != null)
  pstmt.close();
  } catch (Exception stmte) {
  stmte.printStackTrace();
  }

  try {
  if (conn != null)
  conn.close();
  } catch (Exception conne) {
  conne.printStackTrace();
  }
 }
 
}
